/*
 * @Author: zipeng 1134348223@qq.com
 * @Date: 2024-11-03 19:33:45
 * @LastEditors: zipeng 1134348223@qq.com
 * @LastEditTime: 2025-09-23 00:40:25
 * @FilePath: \mycode\input.c
 * @Description: 
 * 
 * Copyright (c) 2024 by HuaWeiTecho, All Rights Reserved. 
 */
#include <string.h>
#include <stdio.h>
#include "stdlib.h"
#include "stdint.h"
#include "def.h"

#if 0
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
    usart_data_transmit(EVAL_COM0, (uint8_t)ch);
    while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
    return ch;
}
#endif

// 1. 定义一个结构体来表示一个节点
typedef struct {
    const char* name;
    void (*func)(char *arg1, char *arg2, char *arg3);
} func_t;

void funcAskVer(char *arg1, char *arg2, char *arg3) {
    if (arg1 == NULL || arg2 == NULL || arg3 == NULL) {
        printf("invalid arguments\r\n");
        return;
    }
    printf("arg1: %s ", arg1);
    printf("arg2: %s ", arg2);
    printf("arg3: %s\r\n", arg3);
    printf("version: 1.0.0\r\n");
}

#define VERSION 1.0
void funcKK(char *arg1, char *arg2, char *arg3) {
    if (arg1 == NULL || arg2 == NULL || arg3 == NULL) {
        printf("invalid arguments\r\n");
        return;
    }
    printf("VERSION\r\n");
}

func_t funcNsmr_List[] = {
    {"ver", funcAskVer},
    {"?", funcKK},
    {"set", NULL},
    {"get", NULL},
    {"del", NULL},
    {NULL, NULL}
};

// 2. 定义一个函数来匹配命令
int mainCpp(int argc, char **argv)
{
    printf("--------main cli----\r\n");
    while (1)
    {
//        typedef struct
//        {
//            int a;
//            int b;
//            uint8_t c;
//            float d;
//        } Mystru;
//        Mystru mystru;
//        printf("c bisa is:%d\r\n", STRU_BIAS(mystru, c));

        char getstr[1024];
        fgets(getstr, sizeof(getstr), stdin);
        printf("%s, %d\r\n", getstr, strlen(getstr));
        char split[2] = " ";
        char *token = NULL;
        char *str1 = NULL;
        char *str2 = NULL;
        char *str3 = NULL;
        token = strtok(getstr, split);
        while (token != NULL) {
            str1 = token;
            token = strtok(NULL, split);
            str2 = token;
            token = strtok(NULL, split);
            str3 = token;
            printf("str1:%s, str2:%s, str3:%s\r\n", str1, str2, str3);
            break;  // 仅取前3个参数，如果有多个参数，需要修改此代码
        }

        for (int i = 0; i < sizeof(funcNsmr_List) / sizeof(funcNsmr_List[0]); i++) {
            if (strstr(getstr, funcNsmr_List[i].name) != NULL) {
                printf("i:%d name:%s, %s\r\n", i, funcNsmr_List[i].name, getstr);
                funcNsmr_List[i].func(str1, str2, str3);
                break;
            }
        }
    }
    return 0;
}
